<?php


namespace wechat\lib\core;


use wechat\lib\Http;

/**
 * Class User
 * @package wechat\lib\core
 * Desc:
 * User: Taurus12C
 * Date: 2020/4/14
 * Time: 15:54
 */
class User extends Base
{

    /**
     * @desc
     * @var
     */
    static private $instance;

    /**
     * User constructor.
     * @param $appid
     * @param $appsecret
     */
    private function __construct($appid, $appsecret)
    {
        $this->appid = $appid;
        $this->appsecret = $appsecret;
    }

    /**
     * @desc: 防止__clone
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    /**
     * @desc: 获取类对象
     * @param $appid
     * @param $appsecret
     * @return User
     */
    static public function getInstance($appid, $appsecret)
    {
        //判断$instance是否是Singleton的对象，不是则创建
        if (!self::$instance instanceof self) {
            self::$instance = new self($appid,$appsecret);
        }
        return self::$instance;
    }

    /**
     * @desc 创建用户分组
     * @param string $name 分组名
     * @return string
     */
    public function createTag(string $name):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=".$this->getGlobalAccessToken();
        $data = [
            'tag' => [
                'name'=>$name
            ]
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 获取标签列表
     * @return string
     */
    public function tagList():string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=".$this->getGlobalAccessToken();
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 修改标签名称
     * @param string $id 标签ID
     * @param string $name 分组名
     * @return string
     */
    public function upTag(string $id,string $name):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=".$this->getGlobalAccessToken();
        $data = [
            'tag' => [
                'id' => $id,
                'name' => $name
            ]
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 删除标签
     * @param int $id 分组ID
     * @return string
     */
    public function delTag(int $id):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=".$this->getGlobalAccessToken();
        $data = [
            'tag' => [
                'id' => $id,
            ]
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }


    /**
     * @desc 获取标签下粉丝列表
     * @param string $id 标签id
     * @param string $offset 第一个拉取的OPENID，不填默认从头开始拉取
     * @return string
     */
    public function tagUserList(string $id,string $offset=""):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=".$this->getGlobalAccessToken();
        $data = [
            'tagid' => $id,
            'next_openid' => $offset
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 批量为用户打标签
     * @param array $openid_list 粉丝列表openid
     * @param string $id 标签ID
     * @return string
     */
    public function tagUser(array $openid_list, string $id):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".$this->getGlobalAccessToken();
        $data = [
            "openid_list" => $openid_list,
            "tagid" => $id
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 批量为用户取消标签
     * @param array $openid_list 粉丝列表openid
     * @param string $id 标签ID
     * @return string
     */
    public function tagUserDel(array $openid_list, string $id):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=".$this->getGlobalAccessToken();
        $data = [
            "openid_list" => $openid_list,
            "tagid" => $id
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 获取用户身上的标签列表
     * @param string $openid
     * @return string
     */
    public function userTag(string $openid):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=".$this->getGlobalAccessToken();
        $data = [
            'openid' => $openid
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 指定用户设置备注名
     * @param string $openid
     * @param string $remark 备注名
     * @return string
     */
    public function userRemark(string $openid, string $remark):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=".$this->getGlobalAccessToken();
        $data = [
            'openid' => $openid,
            'remark' => $remark
        ];
        $str = Http::curl('post',$url,json_encode($data));
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 获取用户基本信息(UnionID机制)
     * @param string $openid
     * @param string $language 国家地区语言版本，zh_CN 简体，zh_TW 繁体，en 英语
     * @return string
     */
    public function userBasicInfo(string $openid, string $language):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->getGlobalAccessToken()."&openid=$openid&lang=$language";
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 获取帐号的关注者列表
     * @param string $next_openid 第一个拉取的OPENID，不填默认从头开始拉取
     * @return string
     */
    public function getUserList(string $next_openid=""):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->getGlobalAccessToken()."&next_openid=$next_openid";
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 获取公众号的黑名单列表
     * @return string
     */
    public function getBlackList():string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist?access_token=".$this->getGlobalAccessToken();
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc: 拉黑用户
     * @param array $openid_list 需要拉入黑名单的用户的openid，一次拉黑最多允许20个
     * @return bool
     */
    public function setBlackList(array $openid_list):bool
    {
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchblacklist?access_token=".$this->getGlobalAccessToken();
        $data = [
            "openid_list"=>$openid_list
        ];
        $str = Http::curl('post',$url,json_encode($data));
        return $this->checkError($str);
    }

    /**
     * @desc: 取消拉黑用户
     * @param array $openid_list 需要拉入黑名单的用户的openid，一次拉黑最多允许20个
     * @return bool
     */
    public function delBlackList(array $openid_list):bool
    {
        $url = " https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist?access_token=".$this->getGlobalAccessToken();
        $data = [
            "openid_list"=>$openid_list
        ];
        $str = Http::curl('post',$url,json_encode($data));
        return $this->checkError($str);
    }
















































































}